home *** CD-ROM | disk | FTP | other *** search
/ Mac Power 1997 December / MACPOWER-1997-12.ISO.7z / MACPOWER-1997-12.ISO / AMUG / PROGRAMMING / Raven 1.2.sit / Raven 1.2 / Source / Foundation / Common / ZDebug.h < prev    next >
Text File  |  1997-05-17  |  5KB  |  146 lines

  1. /*
  2.  *  File:       ZDebug.h
  3.  *  Summary:       Debugging functions and macros.
  4.  *  Written by: Jesse Jones
  5.  *
  6.  *  Copyright ゥ 1996-1997 Jesse Jones. 
  7.  *    For conditions of distribution and use, see copyright notice in ZTypes.h  
  8.  *
  9.  *    Abstract:    This file provides the following macros:
  10.  *
  11.  *                ASSERT(x) - If x is false and DEBUG is true drops into the 
  12.  *                debugger, if ASSERTS_THROW is true throws a TAssertException exception.
  13.  *
  14.  *                VERIFY(x) - Like ASSERT except that x is evaluated in release builds.
  15.  *
  16.  *                DEBUGSTR(formatStr, n1, n2, ...) - Drops into the debugger and
  17.  *                displays a string.
  18.  *
  19.  *                TRACE(formatStr, n1, n2, ...) - Writes a string to a debug window
  20.  *                without suspending the program.
  21.  *
  22.  *                TRACEFLOW(category, formatStr, n1, n2, ...) - Like TRACE, but with 
  23.  *                the addition of a category string. The category string allows users 
  24.  *                to turn off entire categories of messages. Note that DEBUGSTR, TRACE, 
  25.  *                and TRACEFLOW may be used with boolean expressions by tacking an _IF 
  26.  *                on the end (eg DEBUGSTR_IF(x < 0, "Sqrt was passed a negative argument.")).
  27.  *
  28.  *                PRECONDITION(x)  - Used in conjunction with POSTCONDITION to verify 
  29.  *                the integrity of an object: each public method should include one call to 
  30.  *                PRECONDITION and another to POSTCONDITION. The idea here is to check to 
  31.  *                see if the object stays in a valid state given valid arguments. Note that 
  32.  *                these macros call a method named Invariant. This method should use ASSERT's 
  33.  *                to check to see if the object is in a sane state. See ZInvariant.h for
  34.  *                more details.
  35.  *
  36.  *  Change History (most recent first):    
  37.  *
  38.  *         <4>     5/17/97    JDJ        PRECONDITION casts 'this' to an MInvariant*.
  39.  *         <3>     4/12/97    JDJ        If !DEBUG && ASSERTS_THROW PRECONDITION and POSTCONDITION
  40.  *                                    map to ASSERT (instead of no-ops). If DEBUG PRECONDITION
  41.  *                                    uses new ZCheckInvariant class (see ZInvariant.h).
  42.  *         <2>    12/05/96    JDJ        Exposed the break to debugger functions.
  43.  *         <1>     1/13/96    JDJ        Created.
  44.  */
  45.  
  46. #pragma once
  47.  
  48. #include <Windows.h>
  49.  
  50. #include <Assert.h>
  51.  
  52. #include <ZInvariant.h>
  53.  
  54.  
  55. // Synch with ANSI definitions.
  56. #if DEBUG && defined(NDEBUG)
  57. #error DEBUG and NDEBUG are out of sync!
  58. #endif
  59.  
  60. #if !DEBUG && !defined(NDEBUG)
  61. #error DEBUG and NDEBUG are out of sync!
  62. #endif
  63.  
  64. // Synch with MacApp qDebug.
  65. #if DEBUG != qDebug
  66. #error DEBUG and qDebug are out of sync!
  67. #endif
  68.  
  69.  
  70. extern bool gMonkeyLives;                // true if the monkey is awake
  71.  
  72.  
  73. void BreakToDebugger();                    // these ignore the DEBUG flag so be very careful how you use them!
  74. void BreakStrToDebugger(const char* mesg);
  75.  
  76. #if DEBUG
  77.     extern bool gIntenseDebugging;        // defaults to false
  78.     extern bool gBreakOnAssert;            // defaults to true
  79.     
  80.     WindowRef GetSIOUXWindow();
  81.     
  82.     void DEBUGSTR(const char* format, ...);
  83.     void TRACEFLOW(const char* category, const char* format, ...);
  84.     
  85.     void DEBUGSTR_IF(bool cond, const char* format, ...);
  86.     void TRACE_IF(bool cond, const char* format, ...);        
  87.     void TRACEFLOW_IF(bool cond, const char* category, const char* format, ...);
  88.     
  89.     void AssertFailed(const char* expr, const char* file, int line);
  90.     void SafeAssertFailed(const char* expr, const char* file, int line);
  91.     
  92.     #undef assert
  93.  
  94.     #define ASSERT(x)                        do {if (!(x)) AssertFailed(#x, __FILE__, __LINE__);} while (false)
  95.     #define assert(x)                         ASSERT(x)
  96.     #define VERIFY(x)                        ASSERT(x)
  97.  
  98.     #define    PRECONDITION(x)                    ASSERT(x); ¥
  99.                                             const MInvariant* _object = dynamic_cast<const MInvariant*>(this); ¥
  100.                                             ASSERT(_object != nil); ¥
  101.                                             ZCheckInvariant _check(_object)
  102.     #define    POSTCONDITION(x)                ASSERT(x)
  103.  
  104.     void TRACE(const char* format, ...);        
  105.  
  106.     // Interrupt safe debug macros
  107.     void SAFE_DEBUGSTR(const char* str);
  108.     void SAFE_DEBUGSTR(const char* str, long num);
  109.  
  110.     #define SAFE_ASSERT(x)                    do {if (!(x)) SafeAssertFailed(#x, __FILE__, __LINE__);} while (false)
  111.         
  112. #else    // #if DEBUG
  113.  
  114.     inline void __DUMMY_TRACE__(...)        {}
  115.  
  116.     #define DEBUGSTR                        1 ? ((void) 0) : __DUMMY_TRACE__
  117.     #define    TRACEFLOW                        1 ? ((void) 0) : __DUMMY_TRACE__
  118.  
  119.     #define DEBUGSTR_IF                        1 ? ((void) 0) : __DUMMY_TRACE__
  120.     #define TRACE_IF                        1 ? ((void) 0) : __DUMMY_TRACE__
  121.     #define    TRACEFLOW_IF                    1 ? ((void) 0) : __DUMMY_TRACE__
  122.  
  123. #if ASSERTS_THROW
  124.     void AssertFailed(const char*, const char*, int);
  125.  
  126.     #define ASSERT(x)                        do {if (!(x)) AssertFailed(#x, __FILE__, __LINE__);} while (false)
  127.     #define VERIFY(x)                        ASSERT(x)
  128.  
  129.     #define    PRECONDITION(x)                    ASSERT(x)
  130.     #define    POSTCONDITION(x)                ASSERT(x)
  131. #else
  132.     #define    ASSERT(x)                        ((void) 0)
  133.     #define VERIFY(x)                        do {if (x) ;} while (false)
  134.  
  135.     #define    PRECONDITION(x)                    ((void) 0)
  136.     #define    POSTCONDITION(x)                ((void) 0)
  137. #endif
  138.  
  139.     #define TRACE                            1 ? ((void) 0) : __DUMMY_TRACE__
  140.     
  141.     #define SAFE_DEBUGSTR                    1 ? ((void) 0) : __DUMMY_TRACE__
  142.  
  143.     #define    SAFE_ASSERT(x)                    ((void) 0)
  144. #endif    // DEBUG
  145.  
  146.